package com.enlorenz.core.channel.socket.common.writer;

import java.io.OutputStream;

import com.enlorenz.common.util.CodeUtils;

/**
 * 加入长度的写出者
 * @author MAC
 */
public class LengthAppendSocketWriter implements ISocketWriter{
	//数字的长度
	private Integer numLength=8;
	
	public void setNumLength(Integer numLength) {
		this.numLength = numLength;
	}
	
	public Integer getNumLength() {
		return numLength;
	}


	@Override
	public void write(OutputStream outputStream, byte [] message) throws Exception {
		//获取消息长度
		byte [] lenbytes=CodeUtils.getIntegerBytes(message.length,numLength);
		//组合消息头
		byte [] totalMsg=CodeUtils.gatherBytes(lenbytes, message);
		//System.out.println(new String(totalMsg));
		outputStream.write(totalMsg);
		outputStream.flush();
	}

}
